<?php

namespace api\my\controller;

use api\index\controller\CommonController;
use app\admin\model\FavoriteModel;
use app\admin\model\HomeModel;
use app\admin\model\ShopGoodsRelationModel;
use app\admin\model\TalkModel;
use app\admin\model\VideoModel;
use cmf\controller\RestBaseController;
use think\Request;

//点赞收藏
class PraiseController extends RestBaseController
{
    //点赞或收藏
    public function praise()
    {
        if ($this->request->isPost()) {
            $relation_id   = $this->request->post('relation_id');//id
            $type          = $this->request->post('type');//0：收藏，1：点赞
            $relation_type = $this->request->post('relation_type');//类型；0：聊吧，1：视频，2：首页，3：商城商品，4：活动
            $rule          = config('verify.praise');
            $this->checkValidate(['relation_id' => $relation_id, 'type' => $type, 'relation_type' => $relation_type], $rule);
            $table = $this->checkValid($relation_type, $relation_id);

            $where = [
                'member_id'     => $this->getUserId(),
                'relation_id'   => $relation_id,
                'type'          => $type,
                'relation_type' => $relation_type,
            ];
            $res   = CommonController::praise($where, $table);
            if ($res['flag']) {
                $this->success($res['msg']);
            } else {
                $this->error($res['msg']);
            }
        } else {
            $this->error('请求方式错误');
        }
    }

    //检测是否有效数据
    private function checkValid($relation_type, $relation_id)
    {
        //检查是否有效
        switch ($relation_type) {
            case FavoriteModel::TALK_TYPE://聊吧
                $table = 'talk';
                break;
            case FavoriteModel::VIDEO_TYPE://视频
                $table = 'video';
                break;
            case FavoriteModel::INDEX_TYPE://首页
                $table = 'home';
                break;
            case FavoriteModel::GOODS_TYPE://商城商品
                $table = 'shop_goods';
                break;
            case FavoriteModel::ACTIVITY_TYPE://活动
                $table = 'activity';
                break;
            default:
                $table = 'talk';
        }
        $is_valid = CommonController::findWhereData($table, ['id' => $relation_id], 'id');
        if (!$is_valid) {
            $this->error('数据不存在');
        }
        return $table;
    }

    //取消点赞或收藏
    public function cancelPraise()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = config('verify.praise');
            $this->checkValidate($data, $rule);
            //检测数据是否有效
            $table = $this->checkValid($data['relation_type'], $data['relation_id']);

            $data['member_id'] = $this->getUserId();
            $res               = CommonController::cancelPraise($data, $table);
            if ($res['flag']) {
                $this->success($res['msg']);
            } else {
                $this->error($res['msg']);
            }
        } else {
            $this->error('请求方式错误');
        }
    }

    public function collect($relation_type = 1)
    {
        if (!$this->request->isGet()) {
            $this->error('请求方式错误');
        }
        $where = ['type' => FavoriteModel::COLLECTION, 'relation_type' => $relation_type, 'member_id' => $this->getUserId()];

        if ($relation_type == FavoriteModel::VIDEO_TYPE) { //视频

            $favorite = FavoriteModel::field('id,relation_id')
                ->where($where)
                ->with(['video' => function ($query) {
                    $query->field('id,title,is_rotation,video_url,look_num,create_time');
                }])
                ->order('create_time desc')
                ->paginate()->each(function ($item) {
                    $item->is_collection = true;
                });
            $this->success('成功', $favorite);
        }
        //聊吧文章
        $talk = FavoriteModel::where($where)
            ->with(['talk' => function ($query) {
                $query->field('id,title,talk_coin,read_num,type1,buy_num,praise_num');
                $query->with(['preResult' => function ($query) {
                    $query->field('id,bat_id,talk_id');
                    $query->with(['bat_match' => function ($query) { //关联对战表
                        $query->field('id,lea_name,team_main_id,team_pass_id,match_time');
                        $query->with(['team_main_name', 'team_pass_name']);
                    }]);
                }]);
            }])
            ->order('create_time desc')
            ->paginate();
        $this->success('成功', $talk);
        //首页聊吧
//        $home = FavoriteModel::field('id,relation_id,create_time')
//           ->where(['type'=>FavoriteModel::COLLECTION,'relation_type'=>FavoriteModel::INDEX_TYPE,'member_id'=>$this->getUserId()])
//           ->with(['home'=>function($query){
////               $query->field('')
//               $query->with(['guess'=>function($query){
//                   $query->field('id,lea_name,team_main_id,team_pass_id,match_time');
//                   $query->with(['teamMainName','teamPassName']);
//               }]);
//           }])->select()->each(function ($item){
//                $array = [0=>['bat_match'=>$item['home']['guess']]];
//                $item['home']['pre_result'] = $array;
//                $item['talk'] = $item['home'];
//                unset($item['home']);
//                unset($item['talk']['guess']);
//            })->toArray();

//        //合并数组
//        $data = array_merge($home,$talk);
//        $ages = array();
//        foreach ($data as $v) {
//            $ages[] = $v['create_time'];
//        }
//        //数组排序
//        array_multisort($ages, SORT_DESC, $data);


    }

}